/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * Copyright 2010, Center for Advanced Information Systems, Nanyang Technological University
 *
 * File name: ChooseResult.java
 *
 * Abstract: Construct the selected result graph with its matched subgraph for creating the DOT file
 *
 * Current Version:      0.1
 * Auther:               Jin Changjiu
 * Modified Date:        Mar.3,2010
 *
 */
package gblend.result;

/**
 *
 * @author cjjin
 */
import gblend.adjlistgraph.ALGraph;
import gblend.adjlistgraph.FsgIdEntry;
import java.io.*;

import java.sql.SQLException;
import java.util.Vector;
import gblend.db.DatabaseInfo;
import gblend.exactquery.MatchedGraph;
import gblend.exactquery.NewUllmanVerify;
import gblend.exactquery.QueryEngine;
import gblend.similarity.IdSort;
import gblend.similarity.SimVerify;
import java.util.Collections;

public class ChooseResults {

    private Vector<FsgIdEntry> idlist = new Vector<FsgIdEntry>();
    private Vector<MatchedGraph> matched = new Vector<MatchedGraph>();
    private NewUllmanVerify uv = new NewUllmanVerify();
    private DatabaseInfo db = new DatabaseInfo();
    private ALGraph query = null;
    private SimVerify sv = null;
    private Createdot cdot = new Createdot();
    private Vector<ALGraph> candGraphSet = null;
    private QueryEngine qe = new QueryEngine();

    public void getIDs(QueryEngine qengine, DatabaseInfo dbinfo, Vector<FsgIdEntry> results,
            Vector<MatchedGraph> matchedGraph, ALGraph q, Vector<ALGraph> cands) {
        qe = qengine;
        db = dbinfo;
        query = q;
        matched = matchedGraph;
        candGraphSet = cands;
        idlist.addAll(results);

        /*if (qe.getQuerytype() != 3)//for similarity query ranking based on similarity
        {
            IdSort comp = new IdSort();
            Collections.sort(idlist, comp);
        }*/

        sv = new SimVerify(query);//set query only once

    }

    //is id in matched set?
    public int inMatchedSet(FsgIdEntry id) {
        for (int index = matched.size() - 1; index >= 0; index--) {
            if (id.equals(matched.elementAt(index).getid())) {
                return index;
            }
        }

        return matched.size();
    }

    //select a result id to review from list
    public File selectedResults(FsgIdEntry cbid) throws ClassNotFoundException, SQLException, FileNotFoundException {
        int type = qe.getQuerytype();
        System.out.println("type=" + type + " FsgIdEntry cbid=" + cbid);
        MatchedGraph subgraph = new MatchedGraph();
        ALGraph candidategraph = null;

        if (cbid == null) {
            cbid = idlist.elementAt(0);
        }

        int index = inMatchedSet(cbid);
        System.out.println("cbid=" + cbid + ",index = inMatchedSet(cbid)=" + index + ", matched size =" + matched.size());

        if (index == matched.size())//cbid is not in matched set
        {
            candidategraph = qe.fetchFromMem(cbid);
            candGraphSet.addElement(candidategraph);//add the fetched graph to candidate set

            MatchedGraph tmpg = new MatchedGraph();
            tmpg.setid(cbid);

            //  if(type!=3  &&  uv.verify(query,candidategraph))//8.23if it is exact matching
            if (uv.verify(query, candidategraph))//8.23if it is exact matching
            {
                Vector<Integer> nodeset = uv.getNodeset();
                int nssize = nodeset.size();
                for (int i = 0; i < nssize; i++) {
                    tmpg.addNodes(nodeset.elementAt(i));
                }
                matched.addElement(tmpg);
            } // else if(type==3)//similarity matching
            else//similarity matching
            {
                for (int i = 1; i <= qe.getSigma(); i++) {
                    if (sv.verify(query, candidategraph, i)) {
                        Vector<Integer> nodeset1 = sv.getNodeset();
                        int nssize = nodeset1.size();
                        for (int j = 0; j < nssize; j++) {
                            tmpg.addNodes(nodeset1.elementAt(j));
                        }
                        matched.addElement(tmpg);
                        break;
                    }
                }
            }
        } else//the common infrequent fragments, fetch graph from the candidate graph set
        {
            candidategraph = candGraphSet.elementAt(index);
        }

        //get the matched subgraph from the position of cbid in matched set
        subgraph = matched.elementAt(index);
        //construct the dot file
        cdot.createGraph(db, candidategraph);
        File f = cdot.formatDotFile(subgraph);

        return f;
    }

    public Vector<FsgIdEntry> getList() {
        return idlist;
    }
}
